//
//  EQSwipeCell.swift
//  DSP
//
//  Created by tucici on 2024/5/17.
//

import UIKit
import TCCKit
struct EQSwipeCellModule{
    var name = ""
    var r : NSRange
}
protocol EQSwipeCellDelegate{
    func scrollTo(_ module: EQSwipeCellModule)
    func swipe(_ sign:SignModule)
}
class EQSwipeCell: UITableViewCell {
    private var leftBtn : UIButton!
    private var rightBtn : UIButton!
    private var collectionView : UICollectionView!
    var delegate : EQSwipeCellDelegate?
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        initUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func initUI(){
        backgroundColor = .clear
        selectionStyle = .none
        
        getBtn("swipe_left", &leftBtn)
        getBtn("swipe_right", &rightBtn)
        func getBtn(_ img:String, _ sender:inout UIButton?){
            let btn = UIButton(type: .custom)
            btn.setImage(UIImage(named: img), for: .normal)
            btn.bgHexColor = HEX_BG
            btn.addTarget(self, action: #selector(actionForBtn), for: .touchUpInside)
            sender = btn
        }
        
        let flowlayout = UICollectionViewFlowLayout()
        flowlayout.scrollDirection = .horizontal
        flowlayout.minimumLineSpacing = 0
        flowlayout.minimumInteritemSpacing = 0
        flowlayout.sectionInset = .zero
        flowlayout.itemSize = CGSize(width: kUIScreenWidth - 30.k_w - 30.k_w, height: floor(158.k_h))
        collectionView = UICollectionView(frame:.zero,collectionViewLayout:flowlayout)
        collectionView.backgroundColor = .clear
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.isPagingEnabled = true
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.showsVerticalScrollIndicator = false
        collectionView.register(EQSwipeItem.self)
        
        contentView.addSubview(leftBtn)
        contentView.addSubview(rightBtn)
        contentView.addSubview(collectionView)
        
        leftBtn.snp.makeConstraints { make in
            make.width.equalTo(30.k_w)
            make.left.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        rightBtn.snp.makeConstraints { make in
            make.width.equalTo(30.k_w)
            make.right.equalToSuperview()
            make.centerY.equalToSuperview()
        }
        
        collectionView.snp.makeConstraints { make in
            make.top.bottom.equalToSuperview()
            make.left.equalTo(leftBtn.snp.right)
            make.right.equalTo(rightBtn.snp.left)
            make.height.equalTo(ceil(158.k_h))
        }
    }
    
    
    var array = [EQSwipeCellModule]()
    var model : ChannelModel!{
        didSet{
            collectionView.reloadData()
        }
    }
    
    @objc private func actionForBtn(sender:UIButton){
        let result = (collectionView.contentOffset.x / collectionView.bounds.width)
        if result - CGFloat(Int(result)) > 0.001{return}
        var offsetX = collectionView.contentOffset.x + (sender == leftBtn ?  -collectionView.bounds.width : collectionView.bounds.width)
        offsetX = max(offsetX, 0)
        offsetX = min(offsetX, collectionView.bounds.width * Double(array.count - 1))
        collectionView.setContentOffset(CGPoint(x: offsetX, y: 0), animated: true)
    }
}

//MARK: - UICollectionViewDataSource
extension EQSwipeCell:UICollectionViewDataSource{
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return array.count
    }
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.cell(EQSwipeItem.self, indexPath) as! EQSwipeItem
        let module = array[indexPath.row]
        cell.bgImg.image = UIImage(named:module.name)
        cell.array =  model.signArray.count < module.r.length ? Array([SignModule]()) : Array(model.signArray[module.r.location..<module.r.length])
        cell.block = {[weak self] sign in
            self?.delegate?.swipe(sign)
        }
        return cell
    }
}
//MARK: - UICollectionViewDelegate
extension EQSwipeCell:UICollectionViewDelegate{
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath){
    }
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        if Int(scrollView.contentOffset.x) % Int(scrollView.bounds.width) != 0 {return}
        let page = Int(scrollView.contentOffset.x / scrollView.bounds.width)
        let module = array[page]
        delegate?.scrollTo(module)
    }
}

